//
//  InfoViewController.m
//  小团购
//
//  Created by 樊荣海 on 15/2/4.
//  Copyright (c) 2015年 樊荣海. All rights reserved.
//

#import "InfoViewController.h"

@interface InfoViewController ()

@end

@implementation InfoViewController {


    InfoHeaderView *_header;
    UIScrollView *_scrollView;
    
    UIActivityIndicatorView *_act;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = globalBgColor;
    
    _scrollView = [[UIScrollView alloc] init];
    
    _scrollView.bounds = CGRectMake(0, 0, 430, self.view.frame.size.height);
    _scrollView.alwaysBounceVertical = YES;
    _scrollView.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);
    _scrollView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:_scrollView];

    CGFloat height = 70;
    _scrollView.contentInset = UIEdgeInsetsMake(height, 0, 0, 0);//增加顶部滚动区域
    _scrollView.contentOffset = CGPointMake(0, -height);//向下滚70

    //顶部
    _header = [InfoHeaderView infoHeaderView];
    _header.dealModel = _dealModel;
    _header.frame = CGRectMake(0, 0, _scrollView.frame.size.width, 400);
    [_scrollView addSubview:_header];
    
    //菊花
    _act = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    _act.center = CGPointMake(_scrollView.frame.size.width/2, CGRectGetMaxY(_header.frame)+100);
    _act.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleRightMargin;
    [_scrollView addSubview:_act];
    [_act startAnimating];
    
    //加载detail
    [[DealTool sharedDealTool] dealToolWithDeal_ID:_dealModel.deal_id success:^(DealModel *deal) {
        
        _dealModel = deal;
        _header.dealModel = _dealModel;
        
        //0.结束菊花
        [_act stopAnimating];
        
        //1.[技巧] 根据headerView, 设置contentSize
        _scrollView.contentSize = CGSizeMake(0, CGRectGetMaxY(_header.frame) + 20);
        
        //2.添加商家
        [self addBusinessView];
        
        //3.添加其他textView
        [self addSubTextView];
        
        
    } fail:^(NSError *error) {
        
        [_act stopAnimating];
    }];

}

- (void)addBusinessView {

    BusinessView *business = [BusinessView businessView];
    business.frame = CGRectMake(0, _scrollView.contentSize.height, _scrollView.frame.size.width, business.frame.size.height);
    business.dealModel = _dealModel;
    
    // 4.设置scrollView的ContentSize
    _scrollView.contentSize = CGSizeMake(0, CGRectGetMaxY(business.frame) + 20);
    [_scrollView addSubview:business];
    
}

- (void)addSubTextView {
    
    //1.团购详情
    [self addTextViewWithTitle:@"团购详情" icon:@"ic_content.png" content:_dealModel.details];
    
    // 2.购买须知
    [self addTextViewWithTitle:@"购买须知" icon:@"ic_tip.png" content:_dealModel.restrictions.special_tips];
    
    // 3.重要通知
    [self addTextViewWithTitle:@"重要通知" icon:@"ic_tip.png" content:_dealModel.notice];
    
}

- (void)addTextViewWithTitle:(NSString *)title icon:(NSString *)icon content:(NSString *)content {
    
    if (content.length==0) return;
    
    InfoTextView *textView = [InfoTextView infoTextView];

    textView.frame = CGRectMake(0, _scrollView.contentSize.height, _scrollView.frame.size.width, textView.frame.size.height);

    textView.title = title;
    textView.icon = icon;
    textView.content = content;
    
    // 4.设置scrollView的ContentSize
    _scrollView.contentSize = CGSizeMake(0, CGRectGetMaxY(textView.frame) + 20);
    [_scrollView addSubview:textView];

}



@end
